<template>
  <div>
    <el-form ref="addFormRef" :rules="rules" :model="addForm.data" label-width="120">
      <el-form-item label="物业名称" prop="name">
        <el-input
          v-model="addForm.data.name"
          style="width: 320px"
          placeholder="请输入物业名称"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input
          v-model="addForm.data.address"
          style="width: 320px"
          placeholder="请输入详细地址"
        />
      </el-form-item>
      <el-form-item label="运营模式" prop="operationMode">
        <el-radio-group v-model="addForm.data.operationMode">
          <el-radio v-for="item in modeList.data" :key="item.id" :value="item.id">
            {{ item.name }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input
          v-model="addForm.data.contacts"
          style="width: 320px"
          placeholder="请输入联系人"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input
          v-model="addForm.data.phone"
          style="width: 320px"
          placeholder="请输入联系方式"
        />
      </el-form-item>
      <el-form-item label="抬头名称" prop="drawn">
        <el-input
          v-model="addForm.data.drawn"
          style="width: 320px"
          placeholder="请输入抬头名称"
        />
      </el-form-item>
      <el-form-item label="税号识别号" prop="taxId">
        <el-input
          v-model="addForm.data.taxId"
          style="width: 320px"
          placeholder="请输入税号识别号"
        />
      </el-form-item>
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(addFormRef)">重 置</el-button>
            <el-button class="confirm-btn" @click="submitForm(addFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { getAddProperty } from "../request.js";
const emits = defineEmits(["operationDone"]);

const props = defineProps({
  listData: {
    type: Object,
    default: () => {},
  },
});

const addFormRef = ref("");
const addForm = reactive({
  data: {},
});

const modeList = reactive({
  data: [],
});

const rules = reactive({
  name: [{ required: true, message: "请输入物业名称", trigger: "blur" }],
  address: [{ required: true, message: "请输入详细地址", trigger: "blur" }],
  operationMode:[{ required: true, message: "请选择运营模式", trigger: "change" }],
  contacts: [{ required: true, message: "请输入联系人", trigger: "blur" }],
  phone: [
    { required: true, message: "请输入联系方式", trigger: "blur" },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: "手机号码格式不正确，请重新输入",
      trigger: "blur",
    },
  ],
  drawn: [{ required: true, message: "请输入抬头名称", trigger: "blur" }],
  taxId: [{ required: true, message: "请输入税号识别号", trigger: "blur" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getAddProperty(addForm.data).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = async (formEl) => {
  if (!formEl) return;
  addForm.data = {};
  formEl.resetFields();
};

onMounted(() => {
  // setTimeout(() => {
    modeList.data = props.listData.modeList;
  // }, 1000);
});
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>
